import groovyx.net.http.HTTPBuilder  
import groovyx.net.http.HttpResponseException  
import static groovyx.net.http.Method.GET  
import static groovyx.net.http.ContentType.JSON  
import static groovyx.net.http.ContentType.URLENC 
import org.grails.plugins.lookups.*

class CreateBlogService {

    boolean transactional = true

    def sendCreateBlog(roleInstance,uri) {
        // 调用blog程序，生成教师/学生的blog，blog对应的用户为此教师/学生   
        // START
        try {
            def http = new HTTPBuilder(uri)
            def title = URLEncoder.encode("${roleInstance.name.toString()}",'UTF-8')
            def name = URLEncoder.encode("${roleInstance.name.toString()}",'UTF-8')
            def username
            def classesID
            def role
            if(roleInstance in Student){
                username = URLEncoder.encode("${roleInstance.id}",'UTF-8')
                classesID = roleInstance.classes.id
                role = '2'
            }else if(roleInstance in Teacher){
                username = URLEncoder.encode("${roleInstance.username?.toString()}",'UTF-8')    
                classesID = '0'
                role = '1'
            }
            def welcomeMessage = URLEncoder.encode("${roleInstance.name.toString()}",'UTF-8')
            def schoolName = URLEncoder.encode("${roleInstance.school.name}",'UTF-8') 
            def postBody = [classesId:"${classesID}",hostId:"${roleInstance.id}",schoolId:"${roleInstance.school.id}",name:"${name}",username:"${username}",title:"${title}",subtitle:'',tagLine:'',description:'',welcomeMessageTitle:'',welcomeMessage:"${welcomeMessage}",keyWords:'', siteUrl:'',feedDescription:'', googleAdSenseTop:'',googleAdSenseSide:'',googleAnalytics:'',role:"${role}",schoolName:"${schoolName}"]
            http.post( path: '', body: postBody, 
                       requestContentType: URLENC) { resp, json ->  
                        log.info(resp.statusLine)     
                        log.info("Query response: ") 
                        log.info(json.message)
            }
            return true
        }catch (Exception ex ) {  
            return false 
        }

        ///  END
    }

    def sendCreateClassesBlog(classesInstance,uri) {
        // 调用blog程序，生成教师/学生的blog，blog对应的用户为此教师/学生   
        // START
        try {
            def http = new HTTPBuilder(uri)
            def title = URLEncoder.encode("${classesInstance.level}${Lookup.valueFor('grades', classesInstance.grade)}${Lookup.valueFor('teams', classesInstance.team)}",'UTF-8')
            def name = URLEncoder.encode("${classesInstance.level}${Lookup.valueFor('grades', classesInstance.grade)}${Lookup.valueFor('teams', classesInstance.team)}",'UTF-8')
            def username = URLEncoder.encode("${classesInstance.master.username?.toString()}${classesInstance.id}",'UTF-8')
            def welcomeMessage = URLEncoder.encode("${classesInstance.level}${Lookup.valueFor('grades', classesInstance.grade)}${Lookup.valueFor('teams', classesInstance.team)}",'UTF-8')
            def classesName = URLEncoder.encode("${classesInstance.level}${Lookup.valueFor('grades', classesInstance.grade)}${Lookup.valueFor('teams', classesInstance.team)}",'UTF-8')
            def schoolName = URLEncoder.encode("${classesInstance.school.name}",'UTF-8') 
            def postBody = [classesId:"${classesInstance.id}",hostId:"${classesInstance.id}",schoolId:"${classesInstance.school.id}",name:"${name}",username:"${username}",title:"${title}",subtitle:'',tagLine:'',description:'',welcomeMessageTitle:'',welcomeMessage:"${welcomeMessage}",keyWords:'', siteUrl:'',feedDescription:'', googleAdSenseTop:'',googleAdSenseSide:'',googleAnalytics:'',role:'3',schoolName:"${schoolName}",classesName:"${classesName}",master:"${classesInstance.master.id}"]
            http.post( path: '', body: postBody, 
                       requestContentType: URLENC) { resp, json ->  
                        log.info(resp.statusLine)     
                        log.info("Query response: ") 
                        log.info(json.message)
            }
            return true
         }catch (Exception ex ) {  
            return false 
        }
        ///  END
    }
}
